﻿
namespace Patterns4Net.PatternEnforcer.Core.Patterns
{
    using System;
    using System.ComponentModel.Composition;
    using System.Diagnostics.Contracts;
    using System.Collections.Generic;

    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class PatternCheckerExportAttribute : ExportAttribute
    {
        public PatternCheckerExportAttribute(Type patternType)
            : base(typeof(IPatternChecker))
        {
            Contract.Requires(patternType != null);
            this.PatternType = patternType;
            this.OverridesDefault = false;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PatternCheckerExportAttribute"/> class.
        /// This constructor is used by MEF when it is creating an instance of this metadata.
        /// </summary>
        public PatternCheckerExportAttribute(IDictionary<string, object> data)
        {
            Contract.Requires(data["OverridesDefault"] != null);
            Contract.Requires(data["OverridesDefault"] is bool);
            Contract.Requires(data["PatternType"] != null);
            Contract.Requires(data["PatternType"] is Type);
            Contract.Ensures(this.OverridesDefault == (bool)data["OverridesDefault"]);
            Contract.Ensures(this.PatternType == (Type)data["PatternType"]);

            this.OverridesDefault = (bool)data["OverridesDefault"];
            this.PatternType = (Type)data["PatternType"];
        }

        public bool OverridesDefault { get; set; }

        public Type PatternType { get; private set; }
    }
}
